<template>

	<view ref="taskPopup" class="detail_pop" v-show="isShowDetail" :style="styleData">
		<view  class="pop_title" style="background-color:#dbeff7;">
			<view class="pop_title_item" :class="currentTab == '1' ? 'active':''" @click="handleChangeTab('1')">客户信息</view>
			<view class="pop_title_item" :class="currentTab == '2' ? 'active':''" @click="handleChangeTab('2')">订单信息</view>
		</view>
		<u-form
			labelPosition="left"
			:model="detailData"
			ref="taskForm"
			style="background-color:#f7f7f7;height:100%;overflow-y:auto;"
			:labelStyle='taskFormLabel'
			labelWidth="180"
		>
			<view>
				<u-alert-tips :show-icon="true" type="warning" :title="title" :description="blackDescription" :show="blackShow && isCompanyBlack == 0"></u-alert-tips>
                
                <u-alert-tips :show-icon="true" type="warning" title="" description="已开启黑名单过滤，该号码为黑名单，禁止外呼！" :show="isBlack >= 1"></u-alert-tips>
			</view>
		
			<view v-show="currentTab == '1'" >
				<view style="width:100%;overflow-y:scroll;height:1120rpx;" >
					<u-form-item
						label="客户号码:"
						prop=""
						:borderBottom="false"
						class="scroll-view-item"
					>
						<view style="width:260rpx;"><u-input
							v-model="detailData.phone"
							:border="true"
							:disabled="true"
							:custom-style="inputStyle"
						></u-input></view>

					</u-form-item>
					
					<u-form-item
						v-for="(domain, index) in en_encrypt"
						:key="domain.key"
						:label="domain.label+':'"
						:prop="domain.key"
						:borderBottom="false"
						class="scroll-view-item"
					>
							
						<view style="width:92%;">
							<u-input v-model="domain.value" autocomplete="off" @change="handleChangeRemark(domain,index)" v-if="domain.ds_type == 'text'" :custom-style="inputStyle" :border="true"></u-input>
							
							<u-input type="textarea" v-model="domain.value" autocomplete="off" @change="handleChangeRemark(domain,index)" v-if="domain.ds_type == 'textarea'" :custom-style="inputStyle" :border="true"></u-input>
							
							<radio-group v-model="domain.value" v-if="domain.ds_type == 'radio'"  @change="handleChangeRemark(domain,index)" >
								<label class="radio" v-for="(dict,index) in domain.ds_remark" :key="index+1" style="margin-right:30rpx;">
									<radio :value="dict" style="transform:scale(0.7)" /> {{dict}} 
								</label>
							</radio-group>
								
							<checkbox-group v-if="domain.ds_type == 'checkbox'"  @change="handleChangeCheckboxRemark($event,domain,index)">
								<label class="checkbox" v-for="(dict,index) in domain.ds_remark" :key="index+1" style="margin-right:30rpx;font-size:30rpx;">
									<checkbox :value="dict" style="transform:scale(0.7)" :checked="domain.value.indexOf(dict) == -1 ? false : true" /> {{dict}}
								</label>
							</checkbox-group>
						
						</view>
					</u-form-item>
							
					<u-form-item
						v-for="(domain, index) in en_encrypt_children"
						:key="domain.key + index"
						:prop="domain.key"
						:borderBottom="false"
						class="scroll-view-item"
					>
						<view class="uni-flex uni-column">
							<view class="flex-item flex-item-V">
								<checkbox v-model="domain.value" :checked="domain.value" @change="(val)=>{handleChangeChildrenRemark('','',index,val)}" style="margin-left:20rpx;"> {{ domain.label }}</checkbox>
							</view>
							<view v-for="(item,i) in domain.children" :key="i" class="flex-item flex-item-V">
								<text style="display:block;width:200rpx;float:left;text-align: right;padding-right:10px;">{{ item.label }}</text>
								<u-input v-model="item.value" autocomplete="off" @change="handleChangeChildrenRemark(item,i,index,true)" v-if="item.ds_type == 'text'" :custom-style="inputStyle" :border="true" style="width:calc(100% - 200rpx);float:left;"></u-input>
								<u-input type="textarea" v-model="item.value" autocomplete="off" @change="handleChangeChildrenRemark(item,i,index,true)" v-if="item.ds_type == 'textarea'" :custom-style="inputStyle" :border="true" style="width:calc(100% - 180rpx);float:left;"></u-input>
								
								<view style="width:calc(100% - 200rpx);float:left;">
									<radio-group v-model="item.value" v-if="item.ds_type == 'radio'" @change="handleChangeChildrenRemark(item,i,index,true)" >
										<label class="radio" v-for="(dict,index) in item.ds_remark" :key="index+1" style="margin-right:30rpx;">
											<radio :value="dict" style="transform:scale(0.7)" /> {{dict}} 
										</label>
									</radio-group>
								</view>
								
								<view style="width:calc(100% - 200rpx);float:left;">
									<checkbox-group v-model="item.value" v-if="item.ds_type == 'checkbox'" @change="handleChangeChildrenCheckboxRemark(item,i,index,true)" >
										<label class="checkbox" v-for="(dict,index) in item.ds_remark" :key="index+1" style="margin-right:30rpx;font-size:30rpx;">
											<checkbox :value="dict" style="transform:scale(0.7)" :checked="item.value.indexOf(dict) == -1 ? false : true" /> {{dict}}
										</label>
									</checkbox-group>
								</view>
							</view>
						</view>
					</u-form-item>
				
				</view>
					
				<u-form-item
					label=""
					prop=""
					labewidth="0"
					:borderBottom="false"
					style="position:absolute;bottom:0rpx;width:100%;background-color: #f7f7f7;display: inline-block;"
				>
                    <view style="float:left;width:50%;text-align: left;position: absolute;bottom:20rpx;left:30rpx;">
                        <u-button type="warning" :plain="true" style="float:left;height:70rpx;width:90rpx;margin-right:10rpx;" text="回拨" @click="handleCallBack(detailData.phone)">回拨</u-button>
                        <u-button type="primary" :plain="true" style="float:left;height:70rpx;width:90rpx;margin-right:10rpx;" text="外呼" @click="handleCallPhone()">外呼</u-button>
                        <u-button type="primary" :plain="(isSip == 1 || isSip == '1') ? true:false" style="float:left;height:70rpx;width:90rpx;text-align: center;" text="SIP外呼" :disabled="(isSip == 0 || isSip == '0') ? true:false" @click="handleSipCallPhone(detailData)" >SIP</u-button>
                        
                        <view style="position: absolute;width:160rpx;height:40rpx;background-color: orangered;border-radius: 20rpx;text-align: center;font-size:30rpx;line-height: 40rpx;color:#fff;margin-left:160rpx;margin-top:-45rpx;" v-show="(isSip == 0 || isSip == '0') ? true:false">SIP未注册</view>
                    </view>
                    
					<view style="float:right;width:42%;text-align: right;position: absolute;bottom:20rpx;right:30rpx;">
						<u-button @click="handleCloseCallOrder()" :custom-style="btnFormStyle" type="error" style="float:right;text-align: right;" ><uni-icons type="submit" color="#fff" size="22"></uni-icons> 关闭</u-button>
						<u-button @click="handleSaveCustInfo()" :custom-style="btnFormStyle" :loading="isSubmit" :disabled="isSubmit" type="primary" style="float:right;text-align: right;" ><uni-icons type="submit" color="#fff" size="22"></uni-icons> 保存</u-button>
					</view>
				</u-form-item>
			</view>
		
			<view v-show="currentTab == '2'">
				<scroll-view  scroll-y style="width:100%;height:100%;margin-bottom:250rpx;">
				<!-- <view style="width:100%;overflow-y:scroll;height:auto;margin-bottom:250rpx;" > -->
					<u-form-item
						label="产品:"
						prop=""
						:borderBottom="false"
						labelWidth="120"
					>
						<view style="width:97%;border:orange 1px dashed;float:left;">
							<view style="width:100%;padding:6px 6px 6px 6px;float:left;clear:both;border-bottom:orange 1px dashed;" v-for="(items,index) in detailData.product" :key="index">
								<view style="width:100%;">
									<span>产品:</span>
									
									<uni-icons @click="handleAddFormOrderItem('add')" type="plus-filled" color="#ff5500" size="32" v-if="index == 0" style="display:block;float:right;margin-right:3rpx;"></uni-icons>
									
									<uni-icons type="minus-filled" color="#ff0000" size="28" v-else @click="handleCancelFormOrderItem(index,'add')" style="display:block;float:right;margin-right:3rpx;"></uni-icons>
									
									<u-input v-model="items.product_title" type="select" placeholder='' :border="true" @click="items.eventProductShow = true" :custom-style="inputStyle" />
									<u-select :list="categoryData" mode="mutil-column-auto" value-name="id" label-name="title" v-model="items.eventProductShow" @confirm="(e)=>{eventProductCallback(e,items,index)}"></u-select>
								</view>
								<view style="width:40%;float:left;">
									<span style="display: block;float:left;">数量:</span>
									<view style="display: block;float:left;">
										<u-input
											v-model="items.num"
											type="number"
											placeholder=""
											:border="true"
											:custom-style="inputStyle"
											@blur="(e)=>{handleChangeNum(e,items,index)}"
										></u-input>
									</view>
								</view>
								<view style="width:58%;float:left;margin-left:1%;">
									<span>总价:</span>
									<u-input
										v-model="items.all_price"
										type="number"
										placeholder=""
										:border="true"
										:custom-style="inputStyle"
									></u-input>
								</view>
							</view>
						</view>

					</u-form-item>
					
					
					<u-form-item
						label="号码:"
						prop="phone"
						:borderBottom="false"
						labelWidth="120"
					>
						<view style="width:260rpx;"><u-input
							v-model="detailData.phone"
							:border="true"
							:disabled="true"
							:custom-style="inputStyle"
						></u-input></view>

					</u-form-item>
					<u-form-item
						label="姓名:"
						prop="contact"
						:borderBottom="false"
						labelWidth="120"
					>
						<view style="width:92%;"><u-input
							v-model="detailData.contact"
							placeholder=""
							:border="true"
							:custom-style="inputStyle"
						></u-input></view>
					</u-form-item>
					<u-form-item
						label="地区:"
						prop=""
						:borderBottom="false"
						labelWidth="120"
					>
						<view style="width:92%;">
							<u-input v-model="city_title" type="select" placeholder='' :border="true" @click="eventCityShow = true" :custom-style="inputStyle" />
							<u-select :list="provinceData" mode="mutil-column-auto" value-name="id" label-name="name" v-model="eventCityShow" @confirm="eventCityCallback"></u-select>
						</view>
					</u-form-item>
					<u-form-item
						label="地址:"
						prop="address"
						:borderBottom="false"
						labelWidth="120"
					>
						<view style="width:92%;"><u-input
							v-model="detailData.address"
							:border="true"
							placeholder=''
							:custom-style="inputStyle"
						></u-input></view>
					</u-form-item>
					<u-form-item
						label="备注:"
						prop=""
						:borderBottom="false"
						labelWidth="120"
					>
						<view style="width:92%;"><u-input
							v-model="detailData.remark"
							type="textarea"
							:border="true"
							placeholder=''
							:custom-style="inputStyle"
						></u-input></view>
					</u-form-item>
					
					<u-form-item
						label="状态:"
						prop=""
						:borderBottom="false"
						labelWidth="120"
						v-if="popType == 'order'"
					>
						<view style="width:92%;">
							<radio-group :size="20">
								<label class="radio" v-for="(item,index) in statusDatas" :key="'remind_' + (index+1)" style="margin-right:30rpx;font-size:24rpx;">
									<radio :value="item.value" :checked="(item.value)*1 == (detailData.order_status)*1" style="transform:scale(0.7)" /> {{item.text}}
								</label>
							</radio-group>
						</view>
					</u-form-item>
					
				<!-- </view> -->
				</scroll-view>
				
				<view style="width:100%;height:30rpx;"></view>
				
				<u-form-item
                    label=""
                    prop=""
                    labewidth="0"
                    :borderBottom="false"
                    style="position:absolute;bottom:0rpx;width:100%;background-color: #f7f7f7;display: inline-block;height:110rpx;"
				>
                    <view style="float:left;width:50%;text-align: left;position: absolute;bottom:20rpx;left:30rpx;">
                        <u-button type="warning" :plain="true" style="float:left;height:70rpx;width:90rpx;margin-right:10rpx;" text="回拨" @click="handleCallBack(detailData.phone)">回拨</u-button>
                        <u-button type="primary" :plain="true" style="float:left;height:70rpx;width:90rpx;margin-right:10rpx;" text="外呼" @click="handleCallPhone()">外呼</u-button>
                        <u-button type="primary" :plain="(isSip == 1 || isSip == '1') ? true:false" style="float:left;height:70rpx;width:90rpx;text-align: center;" text="SIP外呼" @click="handleSipCallPhone(detailData.phone)" :disabled="(isSip == 0 || isSip == '0') ? true:false">SIP</u-button>
                        
                        <view style="position: absolute;width:160rpx;height:40rpx;background-color: orangered;border-radius: 20rpx;text-align: center;font-size:30rpx;line-height: 40rpx;color:#fff;margin-left:160rpx;margin-top:-45rpx;" v-show="(isSip == 0 || isSip == '0') ? true:false">SIP未注册</view>
                    </view>
					<view style="float:right;width:42%;text-align: right;position: absolute;bottom:20rpx;right:30rpx;">
						<u-button @click="handleCloseCallOrder()" :custom-style="btnFormStyle" type="error" style="float:right;text-align: right;" ><uni-icons type="submit" color="#fff" size="22"></uni-icons> 关闭</u-button>
						<u-button @click="handleSaveCallOrder()" :custom-style="btnFormStyle" :loading="isSubmit" :disabled="isSubmit" type="primary" style="float:right;text-align: right;"><uni-icons type="submit" color="#fff" size="22"></uni-icons> {{popType == 'order' ? '保存':'下单'}}</u-button>
					</view>
				</u-form-item>
				
			</view>
			
		</u-form>
		
		<u-toast ref="uToastsCallOrder" />
	</view>

</template>

<script>
	import {
		getOrderGroup,
		updateOrderPro,
		updateCustomerInfoGroup,
		getCustomerInfoByTidGroup,
		addOrderPro,
        getClickToDial,
        changeCallNumStatus
	} from "@/api/task.js";
	
	import{
		getAllCategory,
		getCityList
	} from "@/api/index.js";
	
	import {
		mapGetters,mapState
	} from "vuex";
	
	import { deepClone } from "@/utils/utils";
    
    let that;
	
	export default {
		data() {
			return {
				currentScreenHeight:'800rpx',
				
				title: '',
				blackDescription: '',
				blackShow: false,
				
				detailData:{},
				// isShowDetail:false,
				isSubmit:false,
				isShowDateTime:false,
				currentTab:'2',
				
				inputStyle:{
					fontSize:'30rpx',
					lineHeight:'50rpx',
					height:'50rpx',
					float:'left',
					backgroundColor:'#fff'
				},
				
				taskFormLabel:{
					marginLeft:'30rpx',
					fontSize:'30rpx'
				},
				
				btnFormStyle: {
					border: 'none',
					borderRadius: '10rpx',
					color: '#fff',
					height:'70rpx',
					padding:'20rpx 30rpx',
					margin:'0rpx',
					float:'right',
					marginLeft:'15rpx'
				},
				
				statusDatas: [
					{
						text: '已下单',
						value:0
					},
					{
						text: '发货中',
						value:1
					},
					{
						text: '已签收',
						value:2
					}
				],
				
				start: Date.now() - 10 * 24 * 3600000,
				end: Date.now() + 10 * 24 * 3600000,
				
				categoryData:[],
				provinceData:[],
				
				eventCategoryShow:false,
				category_title:'',
				product_title:'',
				
				eventCityShow:false,
				city_title:'',
				
				is_en: false, //是否加密
				en_encrypt: [], //加密数据内容
				en_encrypt_children: [], //分组加密数据
				en_phone: "",
				en_phone_number: "",
				en_contact: "",
				en_address: "",
				
				current_product:{},
				
				btnStyle: {
					border: 'none',
					borderRadius: '20rpx',
					color: '#fff',
					height:'70rpx',
					padding:'20rpx 10rpx',
					margin:'0rpx'
				},
                
                isCompanyBlack:0,
                isBlack:0
			}
		},
		props:['currentData','isShowDetail','styleData','popType'],
		watch: {
		    isShowDetail(newVal, oldVal) {
				// 监听文本框值的改变
				console.log("callorder watch newVal",newVal);
				console.log("callorder watch oldVal",oldVal);
				console.log("currentData watch",this.currentData);

				if(newVal){
					// this.isShowDetail = newVal;
					
					if(this.popType == 'order'){
						this.detailData = this.currentData;
						this.detailData.phone = this.currentData.num;
						this.detailData.phone_number = this.currentData.num;
						this.detailData.product = this.currentData.product_data;
						this.detailData.remark = this.currentData.remark == null ? '':this.currentData.remark;
						
					}else{
						this.detailData = {
							product:[],
							pid:this.currentData.pid,
							tid:this.currentData.tid,
							cid:this.currentData.cid,
							seat:this.currentData.seat,
                            id:this.currentData.id
						};
					}
					
					this.city_title = "";
					this.category_title = "";
					this.product_title = "";
					this.en_encrypt = [];
					this.en_encrypt_children = [];
					this.en_phone = "";
					this.en_phone_number = "";
					this.en_contact = "";
					this.en_address = "";
					
					this.initData();
				}
		    },
            
            isSip(newVal, oldVal) {
            	// 监听文本框值的改变
            	console.log("isSip watch newVal",newVal);
            	console.log("isSip watch oldVal",oldVal);
            },
		},
		computed: {
			...mapGetters(["userInfo","callType","taskType","isSip"]),
			...mapState({
				callType: state => state.task.callType,
                isSip:state => state.task.isSip
			})
		},
        mounted() {
            that = this;
        },
		methods: {
			//添加产品选项
			handleAddFormOrderItem(type){
			    this.detailData.product.push({
			        product_id : 0,
			        product_title : "",
			        num : 0,
			        price : 0,
			        all_price : 0,
			        category_id:0
			    });
			},
			
			//删除产品选项
			handleCancelFormOrderItem(index,type){
			    this.detailData.product.splice(index,1);
			},
			
            //回拨
            handleCallBack(phone){
            	if(phone == ""){
            		this.$refs.uToastsCallOrder.show({
            			title: "请输入号码！",
            			type: 'error',
            			duration: '2000',
            		});
            	}else{
            		//调用接口
            		let params = {
            			'company_id':this.userInfo.companyid,
            			'caller':this.userInfo.seat,
            			'callee':phone,
            			'seat':this.userInfo.seat,
            			'type':5,
                        'pid':this.userInfo.pid
            		}
            		
            		console.log("callorder params",params);
            		
            		//回拨，判断坐席外呼主叫号码是否存在
            		if (this.userInfo.shownum == "" || this.userInfo.shownum == '0') {
            			this.$refs.uToastsCallOrder.show({
            				title: "请设置外呼主叫号码",
            				type: 'error',
            				duration: '3000',
            			});
            			return;
            		} else {
                        
                        //判断是否为开启黑名单
                        if(this.isBlack >= 1 || this.isBlack == '1' || this.isBlack == '2'){
                            let title_str = (this.isBlack == 1 || this.isBlack == '1') ? '已开启黑名单过滤，该号码为第三方黑名单，禁止外呼！':'已开启黑名单过滤，该号码为企业黑名单，禁止外呼！';
                            
                            this.$refs.uToastsCallOrder.show({
                            	title: title_str,
                            	type: 'error',
                            	duration: '1000',
                            });
                            return;
                        }else{
                            params.shownum = this.userInfo.shownum;
                            params.phone = params.callee;
                            params.cid = params.company_id;
                
                            console.info("弹窗回拨--参数",params);
                            this.$console.info("弹窗回拨--参数",params);
                            
                            console.info("弹窗回拨--先停止定时任务 ",params.callee);
                            this.$console.info("弹窗回拨--先停止定时任务 ",params.callee);
                            
                            //先清除任务
                            clearInterval(this.$globalData.taskTimer);
                            this.$globalData.taskTimer = null;
                
                            //先调用回拨接口，有返回结果后保存通话记录
                            getClickToDial(params).then(res => {
                                console.log("getClickToDial res", res);
                                this.$console.info("弹窗回拨--回拨接口成功 返回数据",res);
                                
                                if(res.data.code == 200){
                                    this.$refs.uToastsCallOrder.show({
                                        title: "回拨成功",
                                        type: 'success',
                                        duration: '2000',
                                    });
                                    
                                    //通话状态监听
                                    params.start_time = new Date().getTime();
                                                                
                                    //放入全局参数
                                    this.$globalData.callData = params;                                
                                    
                                    //记录日志
                                    console.info("弹窗回拨--开始外呼 ", params.callee);
                                    this.$console.info("弹窗回拨--开始外呼 ",params.callee);

                                }else{
                                    this.$refs.uToastsCallOrder.show({
                                        title: res.data.msg,
                                        type: 'error',
                                        duration: '3000',
                                    });
                                    
                                    console.info("弹窗回拨--回拨接口失败 返回数据 ", params.callee);
                                    this.$console.info("弹窗回拨--回拨接口失败 返回数据 ", params.callee);
                                    
                                    //开启任务定时器
                                    if(this.taskType == 0 && this.$globalData.taskTimer == null){
                                        this.$globalData.taskTimer = setInterval(()=>{
                                            uni.$emit("handleGetCall");
                                        },2000);
                                    }
                                }
                            }).catch(err => {
                                console.info("弹窗回拨--回拨接口错误 返回数据 err", params.callee,err);
                                this.$console.info("弹窗回拨--回拨接口错误 返回数据 ", params.callee,err);
                                
                                //开启任务定时器
                                if(this.taskType == 0 && this.$globalData.taskTimer == null){
                                    this.$globalData.taskTimer = setInterval(()=>{
                                        uni.$emit("handleGetCall");
                                    },2000);
                                }
                            });
                        }
            		}
            	}
            },
			
            //SIP外呼
            handleSipCallPhone(data){
                console.log("handleSipCallPhone ",data,this.isBlack);
                
                //判断是否为开启黑名单
                if(this.isBlack >= 1 || this.isBlack == '1' || this.isBlack == '2'){
                    let title_str = (this.isBlack == 1 || this.isBlack == '1') ? '已开启黑名单过滤，该号码为第三方黑名单，禁止外呼！':'已开启黑名单过滤，该号码为企业黑名单，禁止外呼！';
                    
                    this.$refs.uToastsCallOrder.show({
                    	title: title_str,
                    	type: 'error',
                    	duration: '1000',
                    });
                }else{
                    
                    uni.setStorageSync("sipPhone",data);
                    
                    this.$console.info("弹窗外呼--SIP外呼 ", data);
                    
                    this.$store.dispatch("setPageCur","websip");
                }
            },
            
			//加载数据
			initHandle(){
                console.log("detaildata",that.detailData);
                
				this.$nextTick(()=>{
					//获取所有产品分类
					getAllCategory({cid:this.userInfo.companyid}).then(res=>{
						console.log("getallcategory res",res);
						this.categoryData = res.data.data;
						
						let data = res.data.data;
						
						if(this.popType != 'order' && data.length){
							if(data[0].children.length){
								this.detailData.product.push({
									'category_id':data[0].id,
									'product_id':data[0].children[0].id,
									'price':data[0].children[0].price,
									'num':1,
									'all_price':data[0].children[0].price,
									'product_title':data[0].children[0].title,
									'eventProductShow':false
								});
							}
						}
					});
				
					//获取地区数据
					getCityList({pid:0}).then(res=>{
						console.log("getcitylist",res);
						this.provinceData = res.data.data;
					});
				})
			},
			
			//初始化
			initData(){
				//获取其它数据
				this.initHandle();
				
				if(this.popType == 'order'){
					this.currentTab = "2";
					this.handleDetailByOrder();
				}else{
					this.currentTab = "1";
					this.handleDetailByCust();
				}
			},
			
			//根据客户数据处理
			handleDetailByCust(){
				let params = this.currentData;
				
				this.en_encrypt = [];
				this.en_encrypt_children = [];
				
				console.log("params:",params);
				
				//获取客户信息
				getCustomerInfoByTidGroup(params).then(res=>{
				    console.log("getCustomerInfoByTidGroup res",res);
                    this.$console.info("getCustomerInfoByTidGroup res",res);
				    // taskCallData.value = res.data;
				    let data = res.data.data;
				
				    if(data != null){
						
						if(data.black_data != null && data.black_data != ""){
							this.blackDescription = '级别：' + (data.black_data.cid == 0 ? '系统黑名单':'企业黑名单') + ', 时间：' + data.black_data.ctime + (data.black_data.remark ? (', 备注：' + data.black_data.remark):'');
							this.blackShow = true;
						}
                        
                        this.isCompanyBlack = data.is_company_black;
                        this.isBlack = data.is_black;
						
				        data.isencrypt = data.isencrypt == null ? 0 : data.isencrypt;
				        data.isen = data.isen == null ? 0 : data.isen;
				
				        if(data.isencrypt == 0 && data.isen == 0){
							//项目任务都不加密
							this.is_en = false;
				        }else{
							//企业账号登录
							if(data.isen == 1){
								//项目加密
								this.is_en = true;
							}else{
								if(data.isencrypt == 1){
									//任务里加密
									this.is_en = true;
								}else{
									this.is_en = false;
								}
							}
				        }
						
						this.detailData.phone = params.num;
						this.detailData.phone_number = params.num;
						
						//客户信息，扩展字段
						//if(data.project_datas != '' && data.project_datas != null ){
							//数据字段
							let ds_data = data.remark == undefined ? [] : JSON.parse(data.remark);
							let ds_data_children = []; //已保存的字段 子字段
				
							//扩展字段
							let p_data = data.project_datas;
							let p_data_children = []; //扩展一级字段 子字段
							
							//模糊匹配姓名，地址
							let current_en_contact = "";
							let current_en_address = "";
						  
							data.remark = "";
							this.detailData.num = "";
							
							this.detailData.product = [];
							
							// this.detailData = data;
							
							console.log("detaildata",this.detailData,this.categoryData);
						  
							this.detailData.domains = [];
							this.detailData.domains_children = [];
							
							console.log("cccccccccccccccccccc",this.detailData);
				
							p_data.forEach((item,index)=>{
								let is_flag = 0; //是否有同字段，0没有，1有
				
								if(item.children.length){
									p_data_children.push(item);
								}else{
									ds_data.forEach((value,i)=>{
										//数组循环
										for(var pl in value){ //数组对象遍历
											if(item.ds_name == pl){
												let current_value = value[pl];
							
												this.detailData.domains.push({
													key: 'remark_' + index,
													label: item.ds_name,
													value: current_value,
													parent_id:item.parent_id,
													ds_type:item.ds_type,
													ds_remark:item.ds_remark ? item.ds_remark.split(",") : '',
													children:item.children
												});
							
												this.en_encrypt.push({
													key: 'remark_' + index,
													label: item.ds_name,
													value: current_value,
													parent_id:item.parent_id,
													ds_type:item.ds_type,
													ds_remark:item.ds_remark ? item.ds_remark.split(",") : '',
													children:item.children
												});
												
												//取第一个地址内容
												console.log("pl "+index,pl);
												if(current_en_address == '' && pl.indexOf("地址") >= 0){
													current_en_address = value[pl];
												}
												
												if(current_en_contact == '' && pl.indexOf("姓名") >= 0){
													current_en_contact = value[pl];
												}
							
												is_flag = 1;
											}
										}
									})
									
									console.log("is_flag",is_flag);
				
									if(is_flag == 0 ){
										this.detailData.domains.push({
											key: 'remark_' + index,
											label: item.ds_name,
											value: item.ds_type == 'checkbox' ? [] : '',
											parent_id:item.parent_id,
											ds_type:item.ds_type,
											ds_remark:item.ds_remark ? item.ds_remark.split(",") : '',
											children:item.children
										});
					
										this.en_encrypt.push({
											key: 'remark_' + index,
											label: item.ds_name,
											value: item.ds_type == 'checkbox' ? [] : '',
											parent_id:item.parent_id,
											ds_type:item.ds_type,
											ds_remark:item.ds_remark ? item.ds_remark.split(",") : '',
											children:item.children
										});
									
									}
								}
							});
				
							ds_data.forEach((item,index)=>{
								for(var pl in item){
									if(item[pl]*1 == 1){
										ds_data_children.push(item);
									}
								}
							})
				
							//分组处理
							if(p_data_children.length){
								p_data_children.forEach((item,index)=>{
									let p_children_data = [];
									let is_check = 0; //判断是否是当前分组数据，比对分组名，0不是，1是
									let current_ds_data_children = []; //当前分组相等的分组保存数据
						
									ds_data_children.forEach((d,i)=>{
										for(let pl in d){
											if(pl == item.ds_name && d[pl]*1 == 1){
												is_check = 1;
							
												current_ds_data_children = d.children;
											}
										}
									});
				
									item.children.forEach((p,v)=>{
										if(p.ds_type == 'radio' || p.ds_type == 'checkbox'){
											p.ds_remark = p.ds_remark ? p.ds_remark.split(",") : [];
										}
				
										if(current_ds_data_children.length){
											let is_flag_children = 0; //是否存在相等的字段名称
											current_ds_data_children.forEach((a,b)=>{
												for(let i in a){
													if(i == p.ds_name){
														//let en_current_value = (p.ds_type == 'text' || p.ds_type == 'textarea') ? (is_en.value == true ? encryptName(a[i]) : a[i]) : a[i];
							
														p_children_data.push({
															key: 'remark_' + v,
															label: p.ds_name,
															value: a[i] == '0' ? '':a[i],
															parent_id:p.parent_id,
															ds_type:p.ds_type,
															ds_remark:p.ds_remark
														});
							
														is_flag_children = 1;
													}
												}
											})
				
											if(is_flag_children == 0){
												p_children_data.push({
													key: 'remark_' + v,
													label: p.ds_name,
													value: p.ds_type == 'checkbox' ? [] :'',
													parent_id:p.parent_id,
													ds_type:p.ds_type,
													ds_remark:p.ds_remark
												});
											}
										}else{
											p_children_data.push({
												key: 'remark_' + v,
												label: p.ds_name,
												value: p.ds_type == 'checkbox' ? [] :'',
												parent_id:p.parent_id,
												ds_type:p.ds_type,
												ds_remark:p.ds_remark
											});
										}
									});
									
									this.detailData.domains_children.push({
										key: 'remark_' + index,
										label: item.ds_name,
										value: is_check,
										parent_id:item.parent_id,
										ds_type:'',
										ds_remark:'',
										children:p_children_data
									});
				
									this.en_encrypt_children.push({
										key: 'remark_' + index,
										label: item.ds_name,
										value: is_check == 1 ? true:false,
										parent_id:item.parent_id,
										ds_type:'',
										ds_remark:'',
										children:p_children_data
									});
								});
							}
				        
							console.log("current_en_contact",current_en_contact);
							console.log("current_en_address",current_en_address);
						
							this.en_contact = current_en_contact;
							this.en_address = current_en_address;
							
							this.detailData.contact = current_en_contact;
							this.detailData.address = current_en_address;
						//}
				    }
				});
			
			},
			
			//根据订单处理
			handleDetailByOrder(){

				this.city_title = this.currentData.province_name + this.currentData.city_name + this.currentData.county_name;
				if(this.city_title == '0' || this.city_title == 0){
					this.city_title = "";
				}
				
				console.log("getordergroup ",this.currentData);
				
				this.en_encrypt = [];
				this.en_encrypt_children = [];
				
				let params = {
					'id':this.currentData.id,
					'pid':this.currentData.pid,
					'tid':this.currentData.tid,
					'phone':this.currentData.phone,
					'company_id':this.userInfo.companyid
				};
				
				console.log("getordergroup params",params);
								
				getOrderGroup(params).then(res=>{
					console.log("getOrderGroup res",res);
					let current_data = res.data.data;
					
					if(current_data.black_data != null && current_data.black_data != ""){
						this.blackDescription = '级别：' + (current_data.black_data.cid == 0 ? '系统黑名单':'企业黑名单') + ', 时间：' + current_data.black_data.ctime + (current_data.black_data.remark ? (', 备注：' + current_data.black_data.remark):'');
						this.blackShow = true;
					}
						
					if(current_data.isencrypt == 0 && current_data.isen == 0){
						//项目任务都不加密
						this.is_en = false;
					}else{
						//企业账号登录
						if(current_data.isen == 1){
							//项目加密
							this.is_en = true;
						}else{
							if(current_data.isencrypt == 1){
								//任务里加密
								this.is_en = true;
							}else{
								this.is_en = false;
							}
						}
					}
					
					this.en_phone = this.currentData.phone;
					this.en_phone_number = this.currentData.phone_number;
					this.en_contact = this.currentData.contact;
					this.en_address = this.currentData.address;
					
					//客户信息，扩展字段
					if(this.currentData.domains != '' && this.currentData.domains != null ){
						console.log("aaaaaaaaaaa");
						//数据字段
						let ds_data = JSON.parse(this.currentData.domains);
						let ds_data_children = []; //已保存的字段 子字段
						
						console.log("bbbbbbbbbbbbbbbb",ds_data);
								
						//扩展字段
						let p_data = current_data.project_datas;
						let p_data_children = []; //扩展一级字段 子字段
						
								
						this.detailData.domains = [];
						this.detailData.domains_children = [];
								
						p_data.forEach((item,index)=>{
						   let is_flag = 0; //是否有同字段，0没有，1有
								
						   if(item.children.length){
								p_data_children.push(item);
						   }else{
								ds_data.forEach((value,i)=>{
									if(item.ds_name == value.label){
										let current_value = value.value;
										//let en_current_value = (item.ds_type == 'text' || item.ds_type == 'textarea') ? (value.value ? encryptName(value.value) :'') : value.value;
								
										this.detailData.domains.push({
											key: 'remark_' + index,
											label: item.ds_name,
											value: value.value,
											parent_id:item.parent_id,
											ds_type:item.ds_type,
											ds_remark:item.ds_remark ? item.ds_remark.split(",") : '',
											children:item.children
										});
								
				
										this.en_encrypt.push({
											key: 'remark_' + index,
											label: item.ds_name,
											value: current_value == '0' ? '':current_value,
											parent_id:item.parent_id,
											ds_type:item.ds_type,
											ds_remark:item.ds_remark ? item.ds_remark.split(",") : '',
											children:item.children
										});
								
										is_flag = 1;
									}
								})
								
								if(is_flag == 0 ){
									this.detailData.domains.push({
										key: 'remark_' + index,
										label: item.ds_name,
										value: item.ds_type == 'checkbox' ? [] : '',
										parent_id:item.parent_id,
										ds_type:item.ds_type,
										ds_remark:item.ds_remark ? item.ds_remark.split(",") : '',
										children:item.children
									});
								
									this.en_encrypt.push({
										key: 'remark_' + index,
										label: item.ds_name,
										value: item.ds_type == 'checkbox' ? [] : '',
										parent_id:item.parent_id,
										ds_type:item.ds_type,
										ds_remark:item.ds_remark ? item.ds_remark.split(",") : '',
										children:item.children
									});
								}
							}
						});
						
								
						ds_data.forEach((item,index)=>{
						   // for(var pl in item){
							  if(item.value*1 == 1){
								 ds_data_children.push(item);
							  }
						   // }
						})
								
						//分组处理
						if(p_data_children.length){
						   p_data_children.forEach((item,index)=>{
							  let p_children_data = [];
							  let is_check = 0; //判断是否是当前分组数据，比对分组名，0不是，1是
							  let current_ds_data_children = []; //当前分组相等的分组保存数据
					 
							  ds_data_children.forEach((d,i)=>{
								 // for(let pl in d){
									if(d.label == item.ds_name && d.value*1 == 1){
									is_check = 1;
								
									current_ds_data_children = d.children;
									}
								 // }
							  });
								
						   item.children.forEach((p,v)=>{
							  if(p.ds_type == 'radio' || p.ds_type == 'checkbox'){
								 p.ds_remark = p.ds_remark ? p.ds_remark.split(",") : [];
							  }
								
							  if(current_ds_data_children.length){
								 let is_flag_children = 0; //是否存在相等的字段名称
								 current_ds_data_children.forEach((a,b)=>{
									// for(let i in a){
									   if(a.label == p.ds_name){
										  //let en_current_value = (p.ds_type == 'text' || p.ds_type == 'textarea') ? (is_en.value == true ? encryptName(a.value) : a.value) : a.value;
								
										  p_children_data.push({
											 key: 'remark_' + v,
											 label: p.ds_name,
											 value: a.value == '0' ? '':a.value,
											 parent_id:p.parent_id,
											 ds_type:p.ds_type,
											 ds_remark:p.ds_remark
										  });
								
										  is_flag_children = 1;
									   }
									// }
								 })
								
								 if(is_flag_children == 0){
									p_children_data.push({
									   key: 'remark_' + v,
									   label: p.ds_name,
									   value: p.ds_type == 'checkbox' ? [] :'',
									   parent_id:p.parent_id,
									   ds_type:p.ds_type,
									   ds_remark:p.ds_remark
									});
								 }
								 }else{
									p_children_data.push({
									   key: 'remark_' + v,
									   label: p.ds_name,
									   value: p.ds_type == 'checkbox' ? [] :'',
									   parent_id:p.parent_id,
									   ds_type:p.ds_type,
									   ds_remark:p.ds_remark
									});
								 }
								
						   });
								
						   this.detailData.domains_children.push({
							  key: 'remark_' + index,
							  label: item.ds_name,
							  value: is_check,
							  parent_id:item.parent_id,
							  ds_type:'',
							  ds_remark:'',
							  children:p_children_data
						   });
								
						   this.en_encrypt_children.push({
							  key: 'remark_' + index,
							  label: item.ds_name,
							  value: is_check == 1 ? true:false,
							  parent_id:item.parent_id,
							  ds_type:'',
							  ds_remark:'',
							  children:p_children_data
						   });
						   });
						}
					}
					
					console.log("en_encrypt",this.en_encrypt);
					console.log("en_encrypt_children",this.en_encrypt_children);
					
					console.log("detailData",this.detailData);
					
				});
			},
			
			//产品分类下拉
			eventProductCallback(e,item,index) {
				console.log("eventCategoryCallback e",e);
				console.log("item",item);
				console.log("index",index);
				
				this.detailData.product[index].category_id = e[0].value;
				this.detailData.product[index].product_id = e[1].value;
				
				this.detailData.product[index].product_title = e[1].label;

				// this.categoryData.forEach((item)=>{
				// 	if(item.id == e[0].value){
				// 		item.children.forEach((res)=>{
				// 			if(res.id == e[1].value){
				// 				this.current_product = res;
				// 			}
				// 		})
				// 	}
				// })
			},
			
			//数量修改，计算价格
			handleChangeNum(e,item,index){
				console.log("handleChangeNum e",e ,this.current_product);
				let detail_data = this.detailData;
				
				detail_data.product[index].all_price = ((detail_data.product[index].price)*e).toFixed(2);
				
				this.detailData = detail_data;
				
				this.$forceUpdate();
				
				console.log("handleChangeNum all_price",e ,this.detailData);
			},
			
			//地区下拉
			eventCityCallback(e){
				this.detailData.province_id = e[0].value;
				this.detailData.city_id = e[1].value;
				
				this.city_title = e[0].label;
				this.city_title += e[1].label;
				
				if(e[2] != undefined){
					this.detailData.county_id = e[2].value;
					this.city_title += e[2].label;
				}
			},
			
			//标签切换
			handleChangeTab(val){
				this.currentTab = val;
			},
			
			//关闭弹窗
			handleCloseCallOrder(){
				this.$emit("closeCallOrder","");
			},
			
			//扩展字段修改
			handleChangeRemark(data,index){				
				let current_remark = this.detailData.domains[index]; //当前扩展字段
				console.log("handleChangeRemark current_remark",current_remark);
				if(data.value != current_remark.value){
				    this.detailData.domains[index] = data.value;
				}
			},
			
			//扩展字段 checkbox
			handleChangeCheckboxRemark(e,data,index){				
				let current_remark = this.detailData.domains[index]; //当前扩展字段
				let current_en_remark = this.en_encrypt[index];
				console.log("handleChangeRemark current_remark",current_remark);
				
				current_remark.value = e.target.value;
				current_en_remark.value = e.target.value;
				
				this.detailData.domains[index] = current_remark;
				this.en_encrypt[index] = current_en_remark;
				
			},
			
			//分组扩展checkbox选择事件
			handleChangeChildrenCheckboxRemark(data,index,group_index,val){				
				let current_remark = this.detailData.domains_children[group_index]['children']; //当前扩展字段
				let current_en_remark = this.en_encrypt_children[group_index]['children'];
				console.log("handleChangeRemark current_remark",current_remark);
				
				current_remark.value = val;
				current_en_remark.value = val;
			},
			
			//分组扩展字段修改
			handleChangeChildrenRemark(data,index,group_index,val){			
			    this.detailData.domains_children[group_index]['children'][index].value = val;
			
			    this.en_encrypt_children[group_index]['children'][index].value = val;
			},
			
			//保存客户数据
			handleSaveCustInfo(){
				//保存信息
				let current_data = this.detailData;
				
				let params = {
					phone: current_data.phone_number,
					seat:this.userInfo.seat,
					domains:JSON.stringify(this.en_encrypt),
					domains_children:JSON.stringify(this.en_encrypt_children),
					tid:current_data.tid,
					id:current_data.id,
					popType:this.popType
				}
                
                console.log("updateCustomerInfoGroup params",params);

				updateCustomerInfoGroup(params).then(res=>{
					if(res.data.code == 0){
						this.$refs.uToastsCallOrder.show({
							title: res.data.msg,
							type: 'success',
							duration: '2000',
						});
					}else{
						this.$refs.uToastsCallOrder.show({
							title: res.data.msg,
							type: 'error',
							duration: '2000',
						});
					}
				});
			},
			
			//保存修改订单数据
			handleSaveCallOrder(){
				let current_data = deepClone(this.detailData);
				
				console.log("handlesavecallorder",this.en_encrypt);
				
				current_data.domains = JSON.stringify(this.en_encrypt);
				current_data.domains_children = JSON.stringify(this.en_encrypt_children);
				
				current_data.product = JSON.stringify(current_data.product);
                
                if(current_data.province_id == null || current_data.province_id == 'null'){
                    current_data.province_id = 0;
                }
                
                if(current_data.city_id == null || current_data.city_id == 'null'){
                    current_data.city_id = 0;
                }
                
                if(current_data.county_id == null || current_data.county_id == 'null'){
                    current_data.county_id = 0;
                }
				
				if(this.popType == 'order'){
					if(current_data.id){
						//更新订单
						updateOrderPro(current_data).then(res=>{
							if(res.data.code == 0){
								this.$refs.uToastsCallOrder.show({
									title: res.data.msg,
									type: 'success',
									duration: '2000',
								});
							}else{
								this.$refs.uToastsCallOrder.show({
									title: res.data.msg,
									type: 'error',
									duration: '2000',
								});
							}
							console.log("updateorder res",res);
						}).catch(err=>{
							console.log("updateorder err",err);
						})
					}
				}else{
					//添加订单
					let params = deepClone(current_data);
					// let product_param = params.product_id.split("_");
					// params.product_id = product_param[0];
				
					params.seat = this.userInfo.seat;
					// params.product = JSON.stringify(params.product);
				
					console.log("onCreateOrderClick params",params);
				
					addOrderPro(params).then(res=>{
						console.log("addOrderPro res",res);
						if(res.data.code == 0){
							this.$refs.uToastsCallOrder.show({
								title: "订单添加成功",
								type: 'success',
								duration: '2000',
							});
						}else{
							this.$refs.uToastsCallOrder.show({
								title: "订单添加失败",
								type: 'error',
								duration: '2000',
							});
						}
					});
				}
			},
			
			//拨打电话外呼
			handleCallPhone(){
				let _this = this;
				
				console.log("handlecallphone detaildata:",_this.detailData);
                
                //调用接口
                let params = {
                    'company_id':_this.userInfo.companyid,
                    'caller':_this.userInfo.seat,
                    'callee':_this.detailData.phone,
                    'seat':_this.userInfo.seat,
                    'type':3
                }
                
                if(_this.popType != 'order'){
                    params.pid = _this.detailData.pid;
                    params.tid = _this.detailData.tid;
                }else{
                    params.pid = _this.currentData.pid;
                    params.tid = _this.currentData.tid;
                }
                
                if(params.callee != '' && params.callee != null && params.callee != undefined){
                
                    //判断是否为开启黑名单
                    if(this.isBlack >= 1 || this.isBlack == '1' || this.isBlack == '2'){
                        let title_str = (this.isBlack == 1 || this.isBlack == '1') ? '已开启黑名单过滤，该号码为第三方黑名单，禁止外呼！':'已开启黑名单过滤，该号码为企业黑名单，禁止外呼！';
                        
                        this.$refs.uToastsCallOrder.show({
                            title: title_str,
                            type: 'error',
                            duration: '1000',
                        });
                        return;
                    }else{
                    
                        console.log("callorder params",params,_this.detailData,_this.currentData);
                        _this.$console.info("弹窗外呼--外呼参数 ",params);
                        
                        // if(_this.currentData.type == 1){
                            _this.$emit("changeNumStatus",_this.currentData);
                        // }
                        
                        console.info("弹窗外呼--先停止定时任务 ",params.callee);
                        _this.$console.info("弹窗外呼--先停止定时任务 ", params.callee);
                        
                        //先清除任务
                        clearInterval(_this.$globalData.taskTimer);
                        _this.$globalData.taskTimer = null;
                        
                        //获取录音目录
                        plus.io.requestFileSystem(
                            plus.io.PRIVATE_DOC, // 私有文档目录（对应 _doc 路径）
                            (fs) => {
                              // 定义目录路径（示例：在 _doc 下创建 myFolder）
                              let dirPath = 'logs';
                                    
                              // 检查并创建目录
                              fs.root.getDirectory(
                                dirPath,
                                { create: true }, // 自动创建目录
                                (entry) => {
                                    console.log('弹窗外呼--创建文件夹成功:',entry.fullPath);
                                    _this.$console.info('弹窗外呼--创建文件夹成功:',entry.fullPath);
                                    uni.setStorageSync("record_files",entry.fullPath);
                        }, function(e) {
                                console.log('弹窗外呼--创建文件夹失败:',e.message);
                                _this.$console.info('弹窗外呼--创建文件夹失败:',e.message);
                            });
                        }, function(e) {                    
                            console.log('弹窗外呼--请求文件系统失败:',e.message);
                            _this.$console.info('弹窗外呼--请求文件系统失败:',e.message);
                        });
            
                        //提前修改号码状态
                        changeCallNumStatus(params).then(res=>{
                            //通话状态监听
                            params.start_time = new Date().getTime();
                            
                            console.info("弹窗外呼--接口修改号码状态 成功", params.callee,res);
                            _this.$console.info("弹窗外呼--接口修改号码状态 成功",params.callee,res);
                            
                            //记录日志
                            console.info("弹窗外呼--开始外呼 ",params.callee);
                            _this.$console.info("弹窗外呼--开始外呼 ",params.callee);
                            
                            //放入全局参数
                            _this.$globalData.callData = params;
                        
                            //自动拨号
                            plus.device.dial(params.callee,false);
                            
                            console.log('外呼，完成');
                            _this.$console.info("弹窗外呼--转到手机系统外呼 ",params);
                        }).catch(err=>{
                            console.info("弹窗外呼--接口修改号码状态 失败 err"+params.callee,err);
                            _this.$console.info("弹窗外呼--接口修改号码状态 失败 err "+params.callee,err);
                            
                            //开启任务定时器
                            if(_this.taskType == 0 && _this.$globalData.taskTimer == null){
                                _this.$globalData.taskTimer = setInterval(()=>{
                                    uni.$emit("handleGetCall");
                                },2000);
                            }
                        });
                    }
                }else{
                    this.$refs.uToastsCallOrder.show({
                    	title: "请输入号码！",
                    	type: 'error',
                    	duration: '2000',
                    });
                }
			},
		}
	}
</script>

<style>
	.pop_title{
		font-size:32rpx;
		width:100%;
		padding-left:2%;
		padding-right:2%;
		height:80rpx;
		font-weight: bold;
		line-height: 80rpx;
		border-bottom:#ccc 1px solid;
	}
	
	.pop_title_item{
		width:auto;
		float:left;
		padding:0 20rpx;
	}
	
	.pop_title .active{
		background-color:#589bff;
		color:#fff;
	}
	
	.detail_mark{
		width:100%;
		height:100%;
		position: absolute;
		z-index: 9999;
		background-color: #eeeeee;
	}
	
	.detail_pop{
		width:94%;
		margin-left:3%;
		border:#ccc 1px solid;
		background-color:'#f7f7f7';
		box-shadow:10rpx 10rpx 30rpx #ccc;
		border-radius:20rpx;
		position: absolute;
		margin-top:-120rpx;
		/* height:1300rpx; */
        height:80%;
		margin-bottom:10%;
		z-index:10000000000000000000000000;
		overflow:hidden;
		/* padding-bottom:50rpx; */
	}
</style>
